// class MyPromise2 {
//   constructor(executor) {
//     // 规定状态
//     this.state = "pending"
//     // 保存 `resolve(res)` 的res值
//     this.value = undefined
//     // 保存 `reject(err)` 的err值
//     this.reason = undefined
//     // 成功存放的数组
//     this.successCB = []
//     // 失败存放的数组
//     this.failCB = []
//
//
//     let resolve = (value) => {
//       if (this.state === "pending") {
//         this.state = "fulfilled"
//         this.value = value
//         this.successCB.forEach(f => f())
//       }
//     }
//     let reject = (reason) => {
//       if (this.state === "pending") {
//         this.state = "rejected"
//         this.reason = reason
//         this.failCB.forEach(f => f())
//       }
//     }
//
//     try {
//       // 执行
//       executor(resolve, reject)
//     } catch (error) {
//       // 若出错，直接调用reject
//       reject(error)
//     }
//   }
//   then(onFulfilled, onRejected) {
//     if (this.state === "fulfilled") {
//       onFulfilled(this.value)
//     }
//     if (this.state === "rejected") {
//       onRejected(this.reason)
//     }
//     if (this.state === "pending") {
//       this.successCB.push(() => { onFulfilled(this.value) })
//       this.failCB.push(() => { onRejected(this.reason) })
//     }
//   }
// }


Promise.all = function (promises) {
  let list = []
  let count = 0
  return new Promise((resolve, reject) => {
    for (let i = 0; i < promises.length; i++) {
      Promise.resolve(promises[i]).then(res => {
        list[i] = res
        count ++
        if (count === promises.length) {
          resolve(list)
        }
      }, err => reject(err))
    }
  })
}


let res = Promise.all([new Promise((resolve,reject) => {
  resolve(111)
}),new Promise((resolve,reject) => {
  resolve(222)
}),new Promise((resolve,reject) => {
  resolve(333)
})])

res.then(v=> console.log(v),r => console.log(r))
